<template>
  <div class="tmpl">
    <!-- 头部标题行信息 -->
    <div class="page-title">
      <span>人员</span><span class="page-title-en">USER</span>
    </div>
    <!-- 正文信息 -->
    <div class="page-content">
      <!-- 搜索按钮表格信息 -->
      <el-form class="page-content-search" v-show="!moreCondition" label-position="right"
        label-suffix="：" size="small" label-width="100px">
        <el-row :gutter="20">
          <el-col :xs="24" :sm="12" :md="12" :lg="6">
            <el-form-item label="用户名">
              <el-input v-model="searchData.username" style="width: 100%;" clearable placeholder="请输入用户名"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="6">
            <el-form-item label="手机号码">
              <el-input v-model="searchData.phone" style="width: 100%;" clearable placeholder="请输入手机号码"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="6">
            <el-form-item label="邮箱">
              <el-input v-model="searchData.email" style="width: 100%;" clearable placeholder="请输入邮箱"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="6">
            <el-form-item label="是否启用">
              <el-select v-model="searchData.status" clearable placeholder="是否启用">
                <el-option label="是" value="12781001"></el-option>
                <el-option label="否" value="12781002"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!-- 高级搜索区域 -->
      <el-row class="more-condition-left" v-show="moreCondition">
        <el-col class="more-query-input" :span="20">
          <el-input placeholder="用户名，手机号" v-model="searchData.queryCondition" @keyup.enter.native="search" />
        </el-col>
        <el-col class="more-query-button" :span="4">
          <div class="more-query-button" @click="search"><i class="el-icon-search"></i></div>
        </el-col>
      </el-row>
      <!-- 表格上方右侧按钮组 -->
      <el-row class="btn-group-right">
        <el-button class="btn-group-right-item" type="primary" icon="el-icon-search"
          size="small" @click="search" v-show="!moreCondition">搜索</el-button>
        <el-button class="btn-group-right-item" type="default" icon="el-icon-refresh"
          size="small" @click="reset" v-show="!moreCondition">重置</el-button>
        <el-button class="btn-group-right-item" type="primary" icon="el-icon-plus"
          size="small" @click="add">新增</el-button>
        <el-button class="btn-group-right-item" type="primary" icon="el-icon-edit"
          size="small" @click="edit">编辑</el-button>
        <el-button class="btn-group-right-item" type="default" icon="el-icon-download"
          size="small" @click="exportDefault">导出</el-button>
        <el-button class="btn-group-right-item" type="primary" icon="el-icon-menu"
          size="small" @click="searchMore">高级搜索</el-button>
      </el-row>
      <!-- 分页表格 -->
      <el-table
        ref="multipleTable"
        :data="tableData"
        v-loading="loading"
        element-loading-background="rgba(255, 255, 255, 0.2)"
        border
        stripe
        tooltip-effect="dark"
        height="650px"
        max-height="650px"
        style="width: 96%; margin-left: 2%;"
        @selection-change="handleSelectionChange"
        @row-click="handleRowClick"
        :header-cell-style="{background:'#53A8FF', color:'#fff'}">
        <el-table-column type="selection" min-width="50"></el-table-column>
        <el-table-column prop="username" label="用户名" fixed min-width="150"></el-table-column>
        <el-table-column prop="nickname" label="昵称"></el-table-column>
        <el-table-column prop="nickname" label="所在部门"></el-table-column>
        <el-table-column prop="phone" label="手机号码" width="150"></el-table-column>
        <el-table-column prop="email" label="邮箱" width="200"></el-table-column>
        <el-table-column prop="sex" label="性别" width="80"></el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="200"></el-table-column>
        <el-table-column prop="deleteFlag" width="100" label="是否启用">
          <template #default="{row}">
            <el-switch
              v-model="row.enable"
              active-color="#53A8FF"
              @change="onSwitchChange(row)">
            </el-switch>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 底部分页信息 -->
    <div class="page-footer">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-size="pageSize"
        :page-sizes="[10, 20, 40, 50, 100]"
        layout="total, sizes, prev, pager, next"
        :total="totalSize">
      </el-pagination>
    </div>
    <div class="clearfix"></div>
    <!-- 新建编辑弹窗 -->
    <brand-dialog :dialogData="childData" :form="childForm"
      :centerDialogVisible="childVisible" v-on:handleEvent="childEvent" />
  </div>
</template>

<script>
import BrandDialog from '@/components/Common/Brand/BrandDialog.vue'
export default {
  name: 'User',
  components: {
    BrandDialog
  },
  data () {
    return {
      totalSize: 0,
      pageSize: 20,
      currentPage: 1,
      loading: false,
      searchData: {},
      tableData: [],
      multipleSelection: [],
      moreCondition: true,
      childData: {
        editTitle: '新增品牌',
        editType: 'add'
      },
      childForm: {},
      childVisible: false
    }
  },
  created () {
    // 调用分页查询接口
    this.loadData()
  },
  mounted () {

  },
  methods: {
    // 分页查询接口
    loadData () {
      this.loading = true
      let obj = Object.assign({}, this.searchData, {currentPage: this.currentPage, pageSize: this.pageSize})
      // console.log(obj)
      this.$apis.queryUserPage(obj).then(res => {
        // console.log(res)
        if (res && res.code === '200') {
          this.totalSize = res.result.total
          this.pageSize = res.result.size
          this.currentPage = res.result.current
          this.tableData = res.result.records
          setTimeout(() => {
            this.loading = false
          }, 500)
        } else {
          console.log(res)
          this.$notify.error({
            title: '错误',
            message: res.message
          })
          this.loading = false
        }
      }).catch(err => {
        console.log(err)
        this.loading = false
        this.$notify.error({
          title: '错误',
          message: '请求异常'
        })
      })
    },
    // 分页查询搜索
    search () {
      this.loadData()
    },
    // 分页查询重置
    reset () {
      this.searchData = {}
      this.loadData()
    },
    // 改变分页数量
    handleSizeChange (val) {
      this.pageSize = val
      this.loadData()
    },
    // 改变当前页数
    handleCurrentChange (val) {
      this.currentPage = val
      this.loadData()
    },
    // 行多选事件
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    // 行点击事件触发多选的样式
    handleRowClick (val) {
      this.$refs.multipleTable.toggleRowSelection(val)
    },
    // 启用停用事件
    onSwitchChange (val) {
      // 启用停用会触发行点击事件，所以再次触发下抵消
      this.$refs.multipleTable.toggleRowSelection(val)
    },
    add () {
      this.childVisible = true
      this.childData = {
        editTitle: '新增品牌',
        editType: 'add'
      }
    },
    edit () {
      // console.log(this.multipleSelection)
      if (this.multipleSelection.length === 1) {
        // 获取明细并且打开弹窗
        let id = this.multipleSelection[0].id
        this.getById(id)
      } else {
        this.$notify({
          title: '警告',
          message: '请选择一条数据',
          type: 'warning'
        })
      }
    },
    // 高级搜索切换显示
    searchMore () {
      this.moreCondition = !this.moreCondition
      this.searchData = {}
    },
    // 导出Excel
    exportDefault () {
      this.loading = true
      let obj = this.searchData
      this.$apis.exportBrand(obj).then(res => {
        if (res.data.type === 'application/json') {
          this.$notify.error({
            title: '错误',
            message: '请求异常'
          })
        } else {
          const link = document.createElement('a')
          const blob = new Blob([res.data], { type: res.headers['content-type'] })
          link.style.display = 'none'
          link.href = URL.createObjectURL(blob)
          link.download = 'User.xlsx'
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link)
          // 延迟1秒loading效果
          setTimeout(() => {
            this.loading = false
          }, 1000)
        }
      }).catch(err => {
        console.log(err)
        this.loading = false
        this.$notify.error({
          title: '错误',
          message: '请求异常'
        })
      })
    },
    // 父组件接收子组件的数据
    childEvent (data) {
      // console.log(data)
      // 关闭弹窗
      this.childVisible = false
      // 清空弹窗
      this.childForm = {}
      // 如果弹框中保存成功后，则刷新TABLE
      if (data.event && data.event === 'confirm') {
        this.loadData()
      }
    },
    // 明细查询接口
    getById (id) {
      this.$apis.getUserById({id: id}).then(res => {
        // console.log(res)
        if (res && res.code === '200') {
          // 弹出编辑弹窗，并且填充form
          this.childVisible = true
          this.childData = {
            editTitle: '编辑品牌',
            editType: 'edit'
          }
          this.childForm = Object.assign({}, this.childForm, res.result)
        } else {
          console.log(res)
          this.$notify.error({
            title: '错误',
            message: res.message
          })
        }
      }).catch(err => {
        console.log(err)
        this.loading = false
        this.$notify.error({
          title: '错误',
          message: '请求异常'
        })
      })
    }
  }
}
</script>

<style scoped>
  .tmpl {
    border-radius: 5px;
    height: 100%;
  }

  /* 填充el-table右侧滚动条上方表头未被填充的空白 */
  /deep/ .el-table th.gutter {
    background-color: #53A8FF !important;
  }

  /* 解决移动端下按钮组错位的问题 */
  .el-button+.el-button {
    margin-left: 0px;
  }
</style>
